package com.fastloan.mgt.application.repay.app;

import com.fastloan.common.model.CheckResult;
import com.fastloan.mgt.application.base.service.ApplicationService;
import com.fastloan.mgt.application.repay.model.ZyApplicationRepaymentPlan;
import com.fastloan.mgt.application.repay.model.vo.AppCurrRepayExt;
import com.fastloan.mgt.application.repay.model.vo.RepayExt;
import com.fastloan.mgt.application.repay.service.ZyApplicationRepaymentPlanService;
import com.fastloan.safe.core.util.ManagerKit;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by gelon on 2017/11/10.
 */
@RestController
@RequestMapping("/api/zy/apprepayment")
public class AppRepaymentController extends BaseController<ZyApplicationRepaymentPlan> {

    @Autowired
    ApplicationService applicationService;

    @Autowired
    ZyApplicationRepaymentPlanService repaymentPlanService;


    /**
     * 查看当前客户的贷款
     */
    @PostMapping
    public ResultTo queryMineApp(String customerId) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("date", new Date());
        map.put("customerId", customerId);
        return dataPage(applicationService, AppCurrRepayExt.class, "queryMineApp", map);
    }


    /**
     * 查看当前客户经理的贷款
     */
    @PostMapping("/managerApp")
    public ResultTo queryCustomerApp(Integer appStatus) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("date", new Date());
        map.put("customerManagerId", ManagerKit.getUserId());
        if (appStatus != null) {
            map.put("appStatus", appStatus);
        }
        return dataPage(applicationService, AppCurrRepayExt.class, "queryMineApp", map);
    }


    /**
     * 查看进件的还款计划
     */
    @PostMapping("/apprepay")
    public ResultTo apprepay(String appId) {
        Map<String, Object> map = new HashMap<>();
        map.put("appId", appId);
        List<RepayExt> repayExtList = repaymentPlanService.queryByAppId(map);
        return new ResultTo().setData(repayExtList);
    }

    /**
     * 还款
     *
     * @param applicationId 进件Id
     * @return 还款结果
     */
    @GetMapping("/repayment/{applicationId}")
    public ResultTo repayment(@PathVariable("applicationId") String applicationId) {
        CheckResult checkResult = repaymentPlanService.repayment(applicationId);
        if (!checkResult.isCheckResult()){
            return new ResultTo(ResultEnum.OPERATION_FAILED,checkResult.getCheckMsg());
        }
        return new ResultTo();
    }
}
